package com.lanhai.javaBean;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import com.lanhai.model.PathLock;
import com.mybatis.dao.PathLockDao;

public class PathLockBean {

	/**
	 * 加载线路锁列表
	 */
	public static List<PathLock> loadData(SqlSessionFactory sqlSessionFactory) {
		List<PathLock> pathLockList = new ArrayList<PathLock>();
		SqlSession sqlSession = sqlSessionFactory.openSession();
		try {
			PathLockDao dao = sqlSession.getMapper(PathLockDao.class);
			List<Map<String, Object>> list = dao.selectAll();
			if (list.size() > 0) {
				for (Map<String, Object> m : list) {
					pathLockList.add(new PathLock((Integer) m.get("item"), (Integer) m.get("lockId"),
							(String) m.get("name"), (String) m.get("key")));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			sqlSession.close();
		}
		return pathLockList;
	}
	
	public static void insertPathLock(SqlSessionFactory sqlSessionFactory){
		SqlSession sqlSession = sqlSessionFactory.openSession();
		try {
        	//InputStream is = ChargerBean.class.getClass().getResourceAsStream("/data/agvList.csv"); 
        	//BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        	String filePath = "d:/agvData/slam_route.csv";
        	BufferedReader reader = new BufferedReader(new FileReader(filePath));//换成你的文件名
            reader.readLine();//第一行信息，为标题信息，不用，如果需要，注释掉
            String line = null;
			PathLockDao dao = sqlSession.getMapper(PathLockDao.class);
            while((line=reader.readLine())!=null){
                String item[] = line.split(",");//CSV格式文件为逗号分隔符文件，这里根据逗号切分
                int items = Integer.parseInt(item[0]);
                String key = item[1];
                String name = item[2];
                
                	Map<String, Object> map = new HashMap<String, Object>();
                	map.put("item", items);
                	map.put("key", key);
                	map.put("name", name);
        			dao.insertPathLock(map);
        			sqlSession.commit();                	
                	//System.out.println("num:" + item[0] + ",x:" + item[1] + "[" + mapX + "]" + ",y:" + item[2]);
                //}
            } 
            reader.close();				
        } catch (Exception e) { 
            e.printStackTrace(); 
        } finally {
	    	sqlSession.close();
	    }
	}
	
	public static void updatePathLock(SqlSessionFactory sqlSessionFactory){
		SqlSession sqlSession = sqlSessionFactory.openSession();
		try {
        	//InputStream is = ChargerBean.class.getClass().getResourceAsStream("/data/agvList.csv"); 
        	//BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        	String filePath = "d:/agvData/slam_route.csv";
        	BufferedReader reader = new BufferedReader(new FileReader(filePath));//换成你的文件名
            reader.readLine();//第一行信息，为标题信息，不用，如果需要，注释掉
            String line = null;
			PathLockDao dao = sqlSession.getMapper(PathLockDao.class);
            while((line=reader.readLine())!=null){
                String item[] = line.split(",");//CSV格式文件为逗号分隔符文件，这里根据逗号切分
                int items = Integer.parseInt(item[0]);
                String key = item[1];
                String name = item[2];
                
                	Map<String, Object> map = new HashMap<String, Object>();
                	map.put("item", items);
                	map.put("key", key);
                	map.put("name", name);
        			dao.updatePathLock(map);
        			sqlSession.commit();                	
                	//System.out.println("num:" + item[0] + ",x:" + item[1] + "[" + mapX + "]" + ",y:" + item[2]);
                //}
            } 
            reader.close();				
        } catch (Exception e) { 
            e.printStackTrace(); 
        } finally {
	    	sqlSession.close();
	    }
	}
}
